import { Howl } from 'howler';

const warnAudio = import('./assets/172404_57493.mp3');

export const playWarnAudio = (function () {
  let sound = null;
  let loop = 0;
  return function (option = {}) {
    option = Object.assign({ loop: 3, volume: 1 }, option);
    loop = typeof option.loop === 'number' ? option.loop : 0;
    if (!sound) {
      sound = new Howl(
        Object.assign(option, {
          src: [warnAudio],
          loop: typeof option.loop === 'boolean' ? option.loop : false,
          autoplay: false,
        })
      );
      sound.once('load', function () {
        sound.play();
      });
      sound.on('end', function () {
        if (loop-- > 0) {
          sound.play();
        }
      });
    } else if (!sound.playing()) {
      sound.play();
    }
  };
})();

if (process.env.NODE_ENV === 'development') {
  window.playWarnAudio = playWarnAudio;
}
